//
//  YXOrderViewController.m
//  跑跑快销
//
//  Created by 杨潇 on 14-5-4.
//  Copyright (c) 2014年 ylife. All rights reserved.
//

#import "YXOrderViewController.h"
#import "YXOrderTableViewCell.h"
#import "YXMonthView.h"
#import "YXCalendarView.h"
#import "YXOrderListViewController.h"

@interface YXOrderViewController ()

@end

@implementation YXOrderViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    _dayBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    _dayBtn.backgroundColor = [UIColor redColor];
    _dayBtn.frame = CGRectMake(220, 5, 34, 34);
    [_dayBtn addTarget:self action:@selector(touchDayBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationController.navigationBar addSubview:_dayBtn];
    
    _monthBtn = [UIButton buttonWithType:UIButtonTypeSystem];
    _monthBtn.backgroundColor = [UIColor redColor];
    _monthBtn.frame = CGRectMake(274, 5, 34, 34);
    [_monthBtn addTarget:self action:@selector(touchMonthBtn) forControlEvents:UIControlEventTouchUpInside];
    [self.navigationController.navigationBar addSubview:_monthBtn];
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
    [_dayBtn removeFromSuperview];
    [_monthBtn removeFromSuperview];
    if (_monthBackBtn) {
        [_monthBackBtn removeFromSuperview];
    }
    if (_dayBackBtn) {
        [_dayBackBtn removeFromSuperview];
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"客户订单";
    _orderVi = [[YXOrderView alloc] init];
    self.view = _orderVi;
    
    [_orderVi.calendar addTarget:self action:@selector(touchCalendarBtn:) forControlEvents:UIControlEventTouchUpInside];
    _isDay = YES;
    
    _orderVi.tabView.delegate = self;
    _orderVi.tabView.dataSource = self;
}

#pragma mark - ButtonsClick
- (void)touchCalendarBtn:(UIButton *)btn
{
    if (_isDay) {
        if (!_dayBackBtn) {
            UIWindow * window = [[UIApplication sharedApplication].windows objectAtIndex:0];
            _dayBackBtn = [UIButton buttonWithType:UIButtonTypeSystem];
            _dayBackBtn.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.3];
            _dayBackBtn.frame = [UIScreen mainScreen].bounds;
            [_dayBackBtn addTarget:self action:@selector(touchDayBackBtn) forControlEvents:UIControlEventTouchUpInside];
            [window addSubview:_dayBackBtn];
            
            YXCalendarView * dayCalendar = [[YXCalendarView alloc] initWithFrame:CGRectMake(0, btn.frame.origin.y + btn.frame.size.height + STARTY, 320, 325) withBlock:^(NSString * str) {
                [_orderVi.calendar setTitle:str forState:UIControlStateNormal];
                _dayBackBtn.hidden = YES;
            }];
            [_dayBackBtn addSubview:dayCalendar];
        }
        _dayBackBtn.hidden = NO;
    }else{
        if (!_monthBackBtn) {
            UIWindow * window = [[UIApplication sharedApplication].windows objectAtIndex:0];
            _monthBackBtn = [UIButton buttonWithType:UIButtonTypeCustom];
            _monthBackBtn.frame = [UIScreen mainScreen].bounds;
            _monthBackBtn.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:.3];
            [_monthBackBtn addTarget:self action:@selector(touchMonthBackBtn) forControlEvents:UIControlEventTouchUpInside];
            [window addSubview:_monthBackBtn];
            
            YXMonthView * monthView = [[YXMonthView alloc] initWithFrame:CGRectMake(0, btn.frame.origin.y + btn.frame.size.height + STARTY, 320, 110) withBlock:^(NSString * dateStr) {
                [_orderVi.calendar setTitle:dateStr forState:UIControlStateNormal];
                _monthBackBtn.hidden = YES;
            }];
            [_monthBackBtn addSubview:monthView];
        }
        _monthBackBtn.hidden = NO;
    }
}

- (void)touchMonthBackBtn
{
    _monthBackBtn.hidden = YES;
}

- (void)touchDayBackBtn
{
    _dayBackBtn.hidden = YES;
}

- (void)touchDayBtn
{
    _isDay = YES;
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM-dd"];
    [_orderVi.calendar setTitle:[formatter stringFromDate:[NSDate date]] forState:UIControlStateNormal];
}

- (void)touchMonthBtn
{
    _isDay = NO;
    NSDateFormatter * formatter = [[NSDateFormatter alloc] init];
    [formatter setDateFormat:@"yyyy-MM"];
    [_orderVi.calendar setTitle:[formatter stringFromDate:[NSDate date]] forState:UIControlStateNormal];
}

#pragma mark - UITableViewDelegate,UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return 10;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString * identifier = @"cell";
    YXOrderTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (nil == cell) {
        cell = [[YXOrderTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
    }
    cell.nameLab.text = @"calnfncs";
    cell.moneyLab.text = @"噢为妇女";
    cell.salesreturnLab.text = @"放那是了";
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 40;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    YXOrderListViewController * orderListVC = [[YXOrderListViewController alloc] init];
    orderListVC.title = @"订单列表";
    [self setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:orderListVC animated:YES];
}

- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender
{
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
